package com.frxuexi.visit.view

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.app.TimePickerDialog
import android.app.TimePickerDialog.OnTimeSetListener
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.DatePicker
import android.widget.EditText
import android.widget.TimePicker
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.visit.R
import com.frxuexi.visit.VisitIntent
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.databinding.ActivityVisitAddBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.util.Calendar

@Route(path = "/visit/VisitAddActivity")
@AndroidEntryPoint
class VisitAddActivity : BaseActivity<ActivityVisitAddBinding,VisitIntent,VisitViewModel>() {
    override val binding: ActivityVisitAddBinding by lazy { ActivityVisitAddBinding.inflate(layoutInflater) }
    override val vm: VisitViewModel by viewModels<VisitViewModel>()
    val calendar = Calendar.getInstance()
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*>->
                    {
                        ToastUtils.showLong("添加成功")
                        finish()
                    }
                    is UiState.onError->{
                        Log.i("lr",it.msg)
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {


        binding.visitaddIvDate.setOnClickListener {
            DatePickerDialog(this,object :OnDateSetListener{
                override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                    val m=if(month+1<10) "0${month+1}" else "${month+1}"
                    val d=if(dayOfMonth<10) "0${dayOfMonth}" else "${dayOfMonth}"
                    binding.visitaddDate.setText("${year}-${m}-${d}")
                }

            },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show()
        }

        binding.visitaddIvStart.setOnClickListener {
            showTime(binding.visitaddStart)
        }
        binding.visitaddIvEnd.setOnClickListener {
            showTime(binding.visitaddEnd)
        }

        binding.visitaddSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.visitaddCar.isEnabled=isChecked
        }
        binding.visitaddName.setText(SPUtils.getInstance().getString(Const.UNAME))
        binding.visitaddSave.setOnClickListener {
            val time =binding.visitaddDate.text.toString()+" "+binding.visitaddStart.text.toString()
            val uid = binding.visitaddPeople.text.toString().toInt()
            val cardId = binding.visitaddCardId.text.toString()
            val phone = binding.visitaddPhone.text.toString()
            val car = binding.visitaddCar.text.toString()
            val purpose = binding.visitaddPurpose.text.toString()
            val checked = binding.visitaddSwitch.isChecked
            val name = binding.visitaddName.text.toString()
            val item = VisitBeanItem(
                time,
                false,
                emptyList(),
                uid,
                null,
                cardId,
                phone,
                car,
                purpose,
                checked,
                "待审核",
                name,0
            )
            vm.sendIntent(VisitIntent.addVisit(item))
        }
    }

    fun showTime(et:EditText){
        TimePickerDialog(this,object :OnTimeSetListener{
            override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
                val h=if(hourOfDay<10) "0${hourOfDay}" else "${hourOfDay}"
                val m=if(minute<10) "0${minute}" else "${minute}"
                et.setText("${h}:${m}:00")
            }

        },calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show()
    }


}